
'''
    Software License

    Copyright (C) 2021-05-24  Xoronos

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
'''

'''
    Liabilities

    The software is provided "AS IS" without any warranty of any kind, either expressed,
    implied, or statutory, including, but not limited to, any warranty that the software
    will conform to specifications, any implied warranties of merchantability, fitness
    for a particular purpose, and freedom from infringement, and any warranty that the
    documentation will conform to the software, or any warranty that the software will
    be error free.

    In no event shall Xoronos be liable for any damages, including, but not limited to,
    direct, indirect, special or consequential damages, arising out of, resulting from,
    or in any way connected with this software, whether or not based upon warranty,
    contract, tort, or otherwise, whether or not injury was sustained by persons or
    property or otherwise, and whether or not loss was sustained from, or arose out of
    the results of, or use of, the software or services provided hereunder.
    
    To request the provided software under a different license you can contact us at
    support@xoronos.com
'''

#####################################################
##                   Introduction                  ##
#####################################################
## This code example shows how to verify if the    ##
## provided hash is correct.                       ##
#####################################################

#####################################################
##                   Pre-requisits                 ##
#####################################################
## The Xoron matrix decompressed data structure    ##
## needs to be mined before running this code      ##
## example. Some examples on how to mine Xoron     ##
## matrices can be found in                        ##
## generate_xrn_matrix_all_rnd.py or               ##
## generate_xrn_matrix_one_third.py                ##
#####################################################

import xrnlib256 as xrn
import lorem_ipsum as li


##########################
## Variable definitions ##
##########################

# library run time settings

settings = xrn.xrn_crypto_extra_settings_t ( )
# all the subfields of settings, can be found in xrn_crypto_extra_settings_t.txt

#############
## Logging ##
#############

# Set the default logging file streams
# errors -> stderr
# warnings -> stdout
# notifications -> stdout
# debug -> stdout

xrn.xrn_set_default_log(  )

# To change the default logging you can use the functions below,
# where fp is the opened file pointer
# 
# xrn_change_error_log( fp )
# xrn_change_warning_log( fp )
# xrn_change_notification_log( fp )
# xrn_change_debug_log( fp )

##########################
## Initialize variables ##
##########################

# Initialize default run time settings
if ( xrn.XSUCCESS != xrn.xrn_load_default_settings( settings ) ) :
    exit( -1 )

decompressed_xrn_matrix_fp = xrn.fopen( "xrn_matrix.xm", "rb" )
hash_fp = xrn.fopen( "hash.xf", "wb" )
input_file_fp = xrn.fopen( "lorem_ipsum.txt", "rb" )

if (( decompressed_xrn_matrix_fp == None ) or ( input_file_fp == None ) or ( hash_fp == None ) ):
    exit( -1 )

###################
## Generate hash ##
###################

print( "generate hash... " )
if ( xrn.XSUCCESS != xrn.xrn_make_hash_wrapper( input_file_fp,     # input file
                                           decompressed_xrn_matrix_fp,      # Xoron matrix file pointer
                                           hash_fp,   # output hash file pointer
                                           settings   # run time settings
      ) ) :
    xrn.fclose( input_file_fp )
    xrn.fclose( decompressed_xrn_matrix_fp )
    xrn.fclose( hash_fp )
    exit( -1 )

if ( ( 0 != xrn.fclose( decompressed_xrn_matrix_fp ) ) or ( 0 != xrn.fclose( hash_fp ) ) 
     or ( 0 != xrn.fclose( input_file_fp ) ) ) :
    exit( -1 )

##################
## Compare hash ##
##################

print( "comparing hash... " )

decompressed_xrn_matrix_fp = xrn.fopen( "xrn_matrix.xm", "rb" )
hash_fp = xrn.fopen( "hash.xf", "rb" )
input_file_fp = xrn.fopen( "lorem_ipsum.txt", "rb" )

if (( decompressed_xrn_matrix_fp == None ) or ( input_file_fp == None ) or ( hash_fp == None ) ):
    exit( -1 )

if ( xrn.XEQUAL == xrn.xrn_check_hash_wrapper( input_file_fp, # input file
                                               hash_fp, # pre computed hash
                                               decompressed_xrn_matrix_fp, # decompressed Xoron matrix
                                               settings # run time settings
      ) ) :
    xrn.fclose( decompressed_xrn_matrix_fp )
    xrn.fclose( hash_fp )
    exit ( xrn.XEQUAL )


if ( ( 0 != xrn.fclose( decompressed_xrn_matrix_fp ) ) or ( 0 != xrn.fclose( hash_fp ) ) ) :
    exit( -1 )

exit ( xrn.XNOT_EQUAL )




